/**
 * Created by Administrator on 2017/5/11.
 */
import {callHotCity, callGroupCity} from 'api/client/home_api'
import {homeUd} from 'store/modules/types'
const state = {
  homeDetail:{
    hotCity: [],
    groupCity: [],
    sortGroupCity: {}
  }
}

// getters
const getters = {
  homeDetailState : state => state.homeDetail
}

// actions
const actions = {
  [homeUd.A.INIT_PARAMS]({commit, state}){
    commit(homeUd.INIT_PARAMS)
  },
  //热门城市
  [homeUd.A.GET_HOT_CITY] ({commit, state }) {
    return new Promise((resolve, reject) => {
      callHotCity().then( (response) =>{
        if(!_.isEmpty(response)){
          commit(homeUd.GET_HOT_CITY,response)
        }
        resolve(response);
      }).catch(error => {
        reject(error);
      });
    })
  },
  //首页所有城市
  [homeUd.A.GET_GROUP_CITY]({commit, state}){
    return new Promise((resolve, reject) => {
      callGroupCity().then( (response) =>{
        if(!_.isEmpty(response)){
          commit(homeUd.GET_GROUP_CITY,response)
          resolve(response);
        }
      }).catch(error => {
        reject(error)
      })
    })
  }
}

// mutations
const mutations = {
  [homeUd.INIT_PARAMS](state){
    state.homeDetail.hotCity = []
    state.homeDetail.groupCity = []
  },
  [homeUd.GET_HOT_CITY](state, hotCity){
    state.homeDetail.hotCity = hotCity
  },
  [homeUd.GET_GROUP_CITY](state, groupCity){
    state.homeDetail.groupCity = groupCity
    for (let i = 65; i <= 90; i++) {
      if (groupCity[String.fromCharCode(i)]) {
        state.homeDetail.sortGroupCity[String.fromCharCode(i)] = groupCity[String.fromCharCode(i)];
      }
    }
  },
}

export default {
  state,
  getters,
  actions,
  mutations
}